package boogui.member;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

public class S_MemberManager extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField tfSearch;
	private JTable datatable;
	private JComboBox cbscSearch;

	private JButton btnSearch;
	private JTable table;
	private JScrollPane scrollPane;

	public S_MemberManager(JFrame p) {
		super(p);
		setModalityType(ModalityType.DOCUMENT_MODAL);

		setTitle("회원 정보 조회");
		setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/menu/member.png")));
		setBounds(100, 100, 605, 651);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			JPanel mainpanel = new JPanel();
			mainpanel.setPreferredSize(new Dimension(580, 550));
			contentPanel.add(mainpanel);
			mainpanel.setLayout(new BorderLayout(0, 0));
			{
				JPanel pnSearch = new JPanel();
				pnSearch.setPreferredSize(new Dimension(10, 100));
				mainpanel.add(pnSearch, BorderLayout.NORTH);
				GridBagLayout gbl_pnSearch = new GridBagLayout();
				gbl_pnSearch.columnWidths = new int[] { 30, 43, 20, 0, 0, 0, 0,
						0, 0, 0, 0, 0, 0, 0, 30, 0, 0, 30, 0 };
				gbl_pnSearch.rowHeights = new int[] { 40, 0, 0 };
				gbl_pnSearch.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
						0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
						0.0, 0.0, 0.0, Double.MIN_VALUE };
				gbl_pnSearch.rowWeights = new double[] { 0.0, 0.0,
						Double.MIN_VALUE };
				pnSearch.setLayout(gbl_pnSearch);
				{
					String[] search = { "전  체", "아이디", "이  름", "핸드폰" };
					cbscSearch = new JComboBox(search);

					cbscSearch.setPreferredSize(new Dimension(70, 20));
					GridBagConstraints gbc_cbscSearch = new GridBagConstraints();
					gbc_cbscSearch.insets = new Insets(0, 0, 0, 5);
					gbc_cbscSearch.anchor = GridBagConstraints.WEST;
					gbc_cbscSearch.gridx = 1;
					gbc_cbscSearch.gridy = 1;
					pnSearch.add(cbscSearch, gbc_cbscSearch);

					tfSearch = new JTextField();
					tfSearch.setPreferredSize(new Dimension(20, 21));
					GridBagConstraints gbc_tfSearch = new GridBagConstraints();
					gbc_tfSearch.fill = GridBagConstraints.HORIZONTAL;
					gbc_tfSearch.gridwidth = 10;
					gbc_tfSearch.insets = new Insets(0, 0, 0, 5);
					gbc_tfSearch.gridx = 3;
					gbc_tfSearch.gridy = 1;
					pnSearch.add(tfSearch, gbc_tfSearch);
					tfSearch.setColumns(10);
					/*
					 * JTable table1 = new JTable();
					 * 
					 * scrollPane.setViewportView(table1);
					 */
					btnSearch = new JButton("검  색");
					btnSearch.setActionCommand("mbsearch");
					btnSearch.addActionListener(new ActionListener() {

						public void actionPerformed(ActionEvent e) {
							// MembersuperviseService ms = new
							// MembersuperviseService();

							MemberVO m = new MemberVO();
							MembersuperviseDAO mdao = new MembersuperviseDAO();
							String mb = (String) cbscSearch.getSelectedItem();
							// System.out.println("Dd");
							// tfSearch.setText(null);
							switch (e.getActionCommand()) {

							case "mbsearch":
								// tfSearch.setText(null);
								tfSearch.requestFocus();
								if (mb.equals("전  체")) {

									String title[] = { "회원번호", "아이디", "이름",
											"생년월일", "선호장르", "핸드폰", "이메일" };
									DefaultTableModel model1 = new DefaultTableModel() {
										@Override
										public boolean isCellEditable(int row,
												int column) {
											return false;// 열수정 불가 } };
										}
									};
									IMemberDAO mado = new MemberDAO();
									model1.setColumnIdentifiers(title);
									
									for (MemberVO v : mado.selectMember()) {
										model1.addRow(new String[] {
												v.getMemno(), v.getMemid(),
												v.getMemname(),
												v.getMembirth(),
												v.getMemgenre(), v.getMemhp(),
												v.getMememail() });

									}

									JTable table1 = new JTable(model1);
								
					
									scrollPane.setViewportView(table1);
									table1.addMouseListener(new MouseAdapter() {
										@Override
										public void mouseClicked(MouseEvent e) {

											JTable table1 = (JTable) e.getSource();
											int row = table1.getSelectedRow(); // 선택행
											int col = table1.getSelectedColumn();// 선택열

											String val = (String) table1.getValueAt(row, col);

											String msg = row +"/"+col+"/"+val;
											JOptionPane.showMessageDialog(null, msg);

										}
									});

								} else if (mb.equals("아이디")) {
									String str = tfSearch.getText();
									String title[] = { "회원번호", "아이디", "이름",
											"생년월일", "선호장르", "핸드폰", "이메일" };
									DefaultTableModel model1 = new DefaultTableModel() {
										@Override
										public boolean isCellEditable(int row,
												int column) {
											return false;// 열수정 불가 } };
										}
									};
									IMembersuperviseDAO msdao = new MembersuperviseDAO();

									model1.setColumnIdentifiers(title);
									for (MemberVO v : msdao.idsearchMember(m,
											str)) {

										model1.addRow(new String[] {
												v.getMemno(), v.getMemid(),
												v.getMemname(),
												v.getMembirth(),
												v.getMemgenre(), v.getMemhp(),
												v.getMememail() });

									}

									JTable table1 = new JTable(model1);

									scrollPane.setViewportView(table1);

								} else if (mb.equals("이  름")) {
									String str = tfSearch.getText();
									String title[] = { "회원번호", "아이디", "이름",
											"생년월일", "선호장르", "핸드폰", "이메일" };
									DefaultTableModel model1 = new DefaultTableModel() {
										@Override
										public boolean isCellEditable(int row,
												int column) {
											return false;// 열수정 불가 } };
										}
									};
									IMembersuperviseDAO msdao = new MembersuperviseDAO();

									model1.setColumnIdentifiers(title);
									for (MemberVO v : msdao.namesearchMember(m,
											str)) {
										model1.addRow(new String[] {
												v.getMemno(), v.getMemid(),
												v.getMemname(),
												v.getMembirth(),
												v.getMemgenre(), v.getMemhp(),
												v.getMememail() });

									}

									JTable table1 = new JTable(model1);

									scrollPane.setViewportView(table1);

								} else if (mb.equals("핸드폰")) {

									String str = tfSearch.getText();
									String title[] = { "회원번호", "아이디", "이름",
											"생년월일", "선호장르", "핸드폰", "이메일" };
									DefaultTableModel model1 = new DefaultTableModel() {
										@Override
										public boolean isCellEditable(int row,
												int column) {
											return false;// 열수정 불가 } };
										}
									};
									IMembersuperviseDAO msdao = new MembersuperviseDAO();

									model1.setColumnIdentifiers(title);
									for (MemberVO v : msdao.hpsearchMember(m,
											str)) {
										model1.addRow(new String[] {
												v.getMemno(), v.getMemid(),
												v.getMemname(),
												v.getMembirth(),
												v.getMemgenre(), v.getMemhp(),
												v.getMememail() });

									}

									JTable table1 = new JTable(model1);

									scrollPane.setViewportView(table1);

								}
							default:

							}

						}

					});
					GridBagConstraints gbc_btnSearch = new GridBagConstraints();
					gbc_btnSearch.insets = new Insets(0, 0, 0, 5);
					gbc_btnSearch.anchor = GridBagConstraints.EAST;
					gbc_btnSearch.gridx = 16;
					gbc_btnSearch.gridy = 1;
					pnSearch.add(btnSearch, gbc_btnSearch);
				}

			}

			scrollPane = new JScrollPane();
			scrollPane.setPreferredSize(new Dimension(180, 400));
			mainpanel.add(scrollPane, BorderLayout.CENTER);

		}

		JPanel buttonPane = new JPanel();
		buttonPane.setPreferredSize(new Dimension(10, 40));
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);

		JButton okButton = new JButton("OK");
		okButton.setActionCommand("OK");
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);

		JButton cancelButton = new JButton("Cancel");
		cancelButton.setActionCommand("Cancel");
		buttonPane.add(cancelButton);

		setVisible(true);

	}
}
